package com.example.mbl.service.impl;

import com.example.mbl.dao.UserMapper;
import com.example.mbl.entity.User;
import com.example.mbl.result.PageResult;
import com.example.mbl.result.WebRestResult;
import com.example.mbl.result.business.UserPageResult;
import com.example.mbl.result.business.UserResult;
import com.example.mbl.service.UserService;
import com.example.mbl.util.JwtUtils;
import com.example.mbl.util.MD5Util;
import com.example.mbl.util.UUIDUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class UserServiceImpl implements UserService {
    @Autowired
    private UserMapper userMapper;

    @Override
    public UserResult loginUser(String username, String password) {
        UserResult result = new UserResult();
        User user = userMapper.findUserByUsername(username);
        if(user!=null && user.getPassword().equals(MD5Util.getMD5Info(password))){
            result.setToken(JwtUtils.generateToken(user.getUserid()));
            result.setUserid(user.getUserid());
            result.setUsername(user.getUsername());
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("用户名或密码错误");
        }
        return result;
    }

    @Override
    public WebRestResult addUser(User user) {
        WebRestResult result = new WebRestResult();
        user.setUserid(UUIDUtil.randomUUID());
        user.setFlag("0");
        if(userMapper.insert(user)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("添加用户失败");
        }
        return result;
    }

    @Override
    public WebRestResult deleteUser(User user) {
        WebRestResult result = new WebRestResult();
        User tuser = userMapper.selectByPrimaryKey(user.getUserid());
        tuser.setFlag("1");
        if(userMapper.updateByPrimaryKeySelective(tuser)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("删除用户失败");
        }
        return result;
    }

    @Override
    public PageResult<UserPageResult> getUserList(int page, int pagesize) {
        PageResult<UserPageResult> result = new PageResult<>();
        int start = (page-1)*pagesize;

        List<UserPageResult> list = userMapper.getUserList(pagesize,start);
        if(list.size()>0){
            result.setPageCount(list.get(0).getPage());
            result.setTotal(list.get(0).getCnt());
            result.setRows(list);
        }
        result.setResult(WebRestResult.SUCCESS);
        return result;
    }

    @Override
    public WebRestResult updateUser(User user) {
        WebRestResult result = new WebRestResult();
        user.setPassword(MD5Util.getMD5Info(user.getPassword()));
        if(userMapper.updateByPrimaryKeySelective(user)==1){
            result.setResult(WebRestResult.SUCCESS);
        }
        else{
            result.setResult(WebRestResult.FAILURE);
            result.setMessage("修改用户失败");
        }
        return result;
    }
}
